package com.hyb.algorithm.data.struct.str;

import java.util.*;

/**
 * @Author: huyanbing
 * @Date: 2021/9/14 11:43 上午
 * <p>
 * 524. 通过删除字母匹配到字典里最长单词
 * https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/
 */
public class FindLongestWord {


    public static void main(String[] args) {

        String s = "abpcplea";
        List<String> dictionary = Arrays.asList(new String[]{"ale", "apple", "monkey", "plea"});

        FindLongestWord exe = new FindLongestWord();
        String res = exe.findLongestWord(s, dictionary);

        System.out.println(res);
    }

    public String findLongestWord(String s, List<String> dictionary) {

        int maxLen = 0;
        String res = "";
        for (String str : dictionary) {

            int dicStrLen = str.length();
            int sLen = s.length();

            int i = 0;
            int j = 0;

            while (i < dicStrLen && j < sLen) {
                if (str.charAt(i) == s.charAt(j)) {
                    i++;
                }
                j++;
            }

            if (i == dicStrLen) {
                //长度大于 或者   （长度相等 但是字符串比它小）
                if (i > maxLen || ((i == maxLen) && (str.compareTo(res) < 0))) {
                    maxLen = i;
                    res = str;
                }
            }
        }

        return res;
    }


}
